<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of FamilyModel
 *
 * @author Bubbuls
 */
require_once 'Classes/Family.php';
require_once 'Classes/Model/PDOModel.php';

class FamilyModel extends PDOModel implements Singleton{
    
     private static $instance;
     
     public function getInstance(){
        if (!isset(self::$instance)) {
            $c = __CLASS__;
            self::$instance = new $c;
        }

        return self::$instance;
     }
     
     public function __clone() {
         throw new Exception("You may not try to clone this object");
     }
 
     protected function __construct() {
        parent::__construct();
    }
    
    public function listFamily(){
	$sql="SELECT * FROM Families";//peut-etre : ORDER BY Name
	$dbData=$this->PDORequest($sql);
        $familyList = array();
	foreach ($dbData as $key => $value) {
            array_push($familyList,new Family($value));
        }
        return $familyList;
    }
    
    public function getFamilyById($id){
        $dbData = $this->PDORequest('SELECT * FROM Families WHERE ID=?',array($id));
        return new Family($dbData[0]);
    }
    
     public function getFamilyByName($name){
        $dbData = $this->PDORequest('SELECT * FROM Families WHERE Name=?',array($name));
        return new Family($dbData[0]);
    }
    
    public function addFamily($family){
	$sql="INSERT INTO `dsgf`.`Families` (`Name`) VALUES (?)";
	$this->PDORequest($sql,array($family->getName()));
	
    }
    
    public function deleteFamily($family){
	$sql="DELETE FROM Families WHERE ID = ?";
	$this->PDORequest($sql,array($family->getId()));
    }
    
    public function deleteFamilyById($id){
	$sql="DELETE FROM Families WHERE ID = ?";
	$this->PDORequest($sql,array($id));
    }
    
    public function editFamily($family){
	$sql="UPDATE Families SET Name = ? WHERE ID = ?";
	$this->PDORequest($sql,array($family->getName(), $family->getId()));
    }
}

?>
